$(function(){
	var url = common.url();
	var began = 0;
	var listlen = 10;
	$('.tan-close').bind('click',function(){
		$('.tandbg').hide();
		$('.tanboxd').hide();
		$('.surd').val('');
		$('.ulnamepac').html('');
		$('.baocuo').html('');
		$('.filed .ulnamepac').html('');
		$('.filed .upbtn span').removeClass('change').html('上传附件');
		$('.updeimg').attr({'src':'assets/front/image/common/nopic.png'})
	});
	$('.addupxm').bind('click',function(){
		$('.tandbg').show();
		$('.tanboxd1').show();
	})
	var jk = '';
	var userid = '';
	var loginurl= common.Interface('onlogin')[0]
	$.ajax({
		url : loginurl,
		type: "POST",
		error: function(request) {
			//console.log(request)
		},
		dataType:"json",
		success: function(data) {
			if(data.ret){
				if(data.data == 'UpFile'){
					
					$.ajax({
						url : '/api/user/getUserId.json',
						type: "POST",
						error: function(request) {
							//console.log(request)
						},
						dataType:"json",
						success: function(data) {
							userid= data;
							jk = '/api/PManage/queryByUserId.json?userId='+ data;
							common.hash('page',function(){fnhash(jk)});
						}
					})
				}else{
					//$('.cont').remove()
					$('.youcede').remove();
					$('.header-box').css({'display':'block'});
					$('.boxbg').css({'display':'block'});
					$('.tanbox').css({'display':'none'});
					$('.login-box').css({'display':'block'});
					$('.showboxtitle').html('登陆');
					$('.baocuo').html('此页面需要上传资料账号...');
					
				}
			}
		}
	})
	
	
	
	function fnhash(jk,hash){
		//分页
		if(common.hashlist().page){
			began = (common.hashlist().page-1)*listlen
		}
		var url = jk +'&offset='+ began +'&limit='+ listlen
		$.ajax({
				url : url,
				type: "POST",
				async: false,
				error: function(request) {
					console.log(request)
				},
				dataType:"json",
				success: function(data) {
						if(data.rows.length>0){
							var json = data;
							var pages =Math.ceil(json.total / listlen);
							common.pagesgn(pages,6);
							$('.xmlist-boxxm').html('');
							var arr = ['初审','审核通过','审核未通过']
							for(var i=0;i<json.rows.length;i++){
								var name = common.maxlen(json.rows[i].name,20,true);
								var html = '<li class="clear">\
									<p class="xmname">'+ name +'</p>\
									<p class="xmstatus">'+ arr[json.rows[i].status] +'</p>\
									<div class="caozuo clear">\
										<p class="change " dataid="'+ json.rows[i].pId +'">修改</p>\
										<a target="_blank" href="'+ json.rows[i].projectUrl +'" class="link ">查看链接</a>\
										<a  href="?li=3&pid='+ json.rows[i].pId +'" class="xmdt-link">项目动态</a>\
									</div></li>';
								$('.xmlist-boxxm').append(html)
							}
						}else{
							$('.pages').css({'display':'none'});
							$('.xmlist-boxxm').html('');
						}
					
					//点击修改，显示内容
					$('.xmlist-box .change').bind('click',function(){
						$('.tandbg').show();
						$('.tanboxd2').show();
						var coid = $(this).attr('dataid');
						$.ajax({
							url : '/api/PManage/query.json?pId='+ coid,
							type: "POST",
							error: function(request) {
								console.log(request)
							},
							dataType:"json",
							success: function(data) {
								if(data.ret){
									var json = data.data;
									$('.forn-up2 input[name="name"]').val(json.name);
									$('.forn-up2 input[name="pId"]').val(json.pId);
									$('.forn-up2 input[name="principal"]').val(json.principal);
									$('.forn-up2 input[name="phone"]').val(json.phone);
									$('.forn-up2 textarea[name="summary"]').val(json.summary);
									$('.forn-up2 input[name="startTime"]').val(json.startTime);
									 $(".forn-up2 select").find("option[value = '"+json.stage+"']").attr("selected","selected");
									$('.forn-up2 .updeimg').attr({'src':json.picture});
									if(json.fiFileName){
										$('.forn-up2 input[name="UpdateFil1"]').parents('.upbtn').children('span').html('修改').addClass('change');
										$('.forn-up2 input[name="UpdateFil1"]').parents('.filed').children('.ulnamepac').html(json.fiFileName);
									}
									if(json.seFileName){
										$('.forn-up2 input[name="UpdateFil2"]').parents('.upbtn').children('span').html('修改').addClass('change');
										$('.forn-up2 input[name="UpdateFil2"]').parents('.filed').children('.ulnamepac').html(json.seFileName);
									}
									if(json.thFileName){
										$('.forn-up2 input[name="UpdateFil3"]').parents('.upbtn').children('span').html('修改').addClass('change');
										$('.forn-up2 input[name="UpdateFil3"]').parents('.filed').children('.ulnamepac').html(json.thFileName);
									}
								}else{}
							}
						})
					})
				}
			})
	}
	
	// 添加
	$('.form-add .tijaio').bind('click',function(){
		var onf = true;
		$('.forn-up1 .surd').each(function(i,e){
			if(e.value == ''){
				onf = false;
				$('.baocuo').html('输入框内容不能为空...');
				return false;
			}
		})
		if(onf){
			$('.surfile').each(function(i,e){
				if(e.value !=''){
					onf = false;
				}
			})
			if(!onf){
				$('#form-upbox1').ajaxSubmit({
					url:'/api/PManage/save.json',
					type:'post',
					data:{userId:userid},
					success:function(data){
						if(data.ret){
							common.hash('page',function(){fnhash(jk)});
							$('.tan-close').trigger('click');
						}
					}
				})
			}else{
				$('.baocuo').html('至少上传一个附件...');
			}
		}
	})
	//修改
	$('.forn-up2 .tijaio').bind('click',function(){
		
		var onf = true;
		$('.forn-up2 .surd').each(function(i,e){
			if(e.value == ''){
				onf = false;
				$('.baocuo').html('输入框内容不能为空...');
				return false;
			}
		});
	
		if(onf){
			$('#form-upbox2').ajaxSubmit({
				url:'/api/PManage/update.json',
				type:'post',
				data:{userId:userid},
				success:function(data){
					if(data.ret){
						common.hash('page',function(){fnhash(jk)});
						$('.tan-close').trigger('click');
					}
				}
			})
		}
	})
})
